class Paging {
	
	pagenum = 1
    locker = false;
    moreData = true;
    accumulator = [];

    constructor() {
		
	}

    async getMoreData() {
		
        if(!this.moreData){
            return
        }
        if(!this._getLocker()){
            return
        }
        const data =await this._actualGetData()
        this._releaseLocker()
        return data
    }


    async _actualGetData(promiseFun) {
        let paging = await promiseFun
        if(!paging){
            return null
        }
        if(paging.length === 0){
            return {
                empty:true,
                items:[],
                moreData:false,
                accumulator:[]
            }
        }

        this.moreData = paging.length <= 20
        this._accumulate(paging)
        return {
            empty:false,
            items: paging,
            moreData:this.moreData,
            accumulator:this.accumulator
        }
    }

    _accumulate(items){
        this.accumulator = this.accumulator.concat(items)
    }


    _getLocker() {
        if (this.locker) {
            return false
        }
        this.locker = true
        return true
    }

    _releaseLocker() {
        this.locker = false
    }

}

export {
    Paging
}
